using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using  UnityEngine.UI;

/// <summary>
/// 格子
/// </summary>
public class BackpackGird : MonoBehaviour , IBeginDragHandler,IDragHandler,IEndDragHandler
{
    public Color enableColor;  // 能放入时颜色提示
    public Color disableColor;  // 不可放入时颜色提示
    public Color defauleColor;  // 默认颜色
    
    private int row;
    private int col;
    private Image img;
    public Sprite defaultImg;
    public Image icon;
    public bool hasItem = false;
    private BackpackGird saveItemGrid;  // 保存当前物品的实际格子
    private BackpackItem saveItem;     // 塞入的物品
    private BackpackManager manager;

    /// <summary>
    /// 初始化的时候 设置格子位置
    /// </summary>
    /// <param name="row"></param>
    /// <param name="col"></param>
    public void InitView(int row,int col,BackpackManager manager)
    {
        this.row = row;
        this.col = col;
        this.manager = manager;
    }

    // Start is called before the first frame update
    void Start()
    {
        this.img = this.transform.GetComponent<Image>();
   
    }

    public void IsEnable()
    {
        this.img.color = enableColor;
    }

    public void IsDisable()
    {
        this.img.color = disableColor;
    }

    /// <summary>
    /// 设置默认图片 （没有物品的时候）
    /// </summary>
    public void SetDefault()
    {
        this.img.color = defauleColor;
    }


    public void RemoveItem()
    {
        this.hasItem = false;
        this.saveItemGrid = null;
        this.saveItem = null;
        this.icon.sprite = defaultImg;
    }

    public bool HasItem()
    {
        return this.hasItem;
    }

    public void SetGirdColor()
    {
        if (this.hasItem)
        {
            IsDisable();
        }
        else
        {
            IsEnable();
        }
    }

    /// <summary>
    /// 设置物品(物品图片)
    /// </summary>
    /// <param name="img"></param>
    public void SetItem(BackpackItem item = null)
    {
        if (item)
        {
            this.icon.sprite = item.GetSprite();
            this.icon.SetNativeSize();
            this.hasItem = true;
            this.saveItem = item;
        }

        this.hasItem = true;
    }

    public BackpackItem GetItem()
    {
        return this.saveItem;
    }

    /// <summary>
    /// 保存当前格子物品 的主格子(默认左上角的)
    /// </summary>
    /// <param name="grid"></param>
    public void SaveItemGird(BackpackGird grid)
    {
        this.saveItemGrid = grid;
    }

    public BackpackGird GetSaveItemGird()
    {
        return this.saveItemGrid;
    }

    /// <summary>
    /// 检测是否有物品
    /// </summary>
    public void CheckHasItem()
    {
        SetGirdColor();
    }

    public void GetRowCol(out int row,out int col)
    {
        row = this.row;
        col = this.col;
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        this.manager.StartDragGrid(eventData,this);
    }

    public void OnDrag(PointerEventData eventData)
    {
        this.manager.InDragGrid(eventData, this);
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        this.manager.EndDragGrid(eventData,this);
    }
}
